/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "@/plugins/ai-warning/file/api/rdFilePlaybackVideo.ts";
import { getFullFileUrl } from "@/utils/fileUrl";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any,
  openVideoPlayer?: (url: string, title: string) => void
): MaProTableColumns[] {
  const msg = useMessage();

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    {
      label: "录像文件",
      prop: "file_url",
      multiple: false,
      align: "center",
      cellRender: ({ row }) => {
        return (
          <el-button
            type="primary"
            size="small"
            onClick={() => {
              if (row?.file_url) {
                const fullUrl = getFullFileUrl(row.file_url);
                const title = `预警视频播放 - ID: ${row.id}`;
                openVideoPlayer?.(fullUrl, title);
              } else {
                msg.info("无文件");
              }
            }}
          >
            <icon-view />
            播放预警视频
          </el-button>
        );
      },
    },
    {
      label: "关联的预警数据id",
      prop: "yj_data_id",
      renderProps: { multiple: false },
    },
    {
      type: "operation",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => hasAuth("file:rdFilePlaybackVideo:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) => hasAuth("file:rdFilePlaybackVideo:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
